/*
	函数库
*/
'use strict';
var AV 				= require('leanengine');
let common		= {};
/**
 * 检测用户是否有管理员权限
 * 2015.04.01  by 胡斌
 * @param page 		当前页码
 * @param pagesize	每页数量
 * @param total		总数量
 */
common.Paginate = function(page, pagesize, total) {
	var pager={};

	pagesize	= parseInt(pagesize);
	page			= parseInt(page);
	if(isNaN(pagesize) || pagesize<1)pagesize=12;
	if(isNaN(page) || page<1)page=1;
	if(isNaN(total) || total<1)total=0;
	//console.log("page="+page);
	pager.firstPage		= 1;
	pager.lastPage		= Math.ceil(total/pagesize)==0?1:Math.ceil(total/pagesize);
	pager.currentPage	= page>pager.lastPage?pager.lastPage:page;
	pager.prevPage		= page-1>1?page-1:1;
	pager.nextPage		= page+1<pager.lastPage?page+1:pager.lastPage;
	pager.pagesize		= pagesize;
	pager.total				= total;
	pager.pageArray		= [];
	//console.dir(pager);
	//if(pager.currentPage<1)pager.currentPage=1;
	for(var i=1;i<=pager.lastPage;i++){
		//console.log(i);
		pager.pageArray.push(i);
	}

	return pager;
}

/**
 * 后台按模板显示页面
 * 2015.04.17  by 胡斌
 * @param res
 * @param req
 * @param message 错误提示
 */
common.sentErr = function(res,req,error,path) {
	//console.log(path);
	if(typeof(error)=="object"){
		error=error.message;
	}
	if(typeof(path)=="undefined"){
		path="";
	}else{
		path=path+"/";
	}
	if(req.body.ajax){
		res.json({error:error});
	}else{
		res.render(path+'message',{
			user:req.AV.user,
			message:error
		});
	}
}

module.exports = common;
